package me.kaede.sample.adapter;

import me.kaede.sample.ResourceTable;
import me.kaede.sample.bean.ListData;
import me.kaede.tagview.Tag;
import me.kaede.tagview.TagView;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/**
 * ListViewAdapter
 *
 * @since 2021-06-07
 */
public class ListViewAdapter extends BaseItemProvider {
    private List<ListData> mListDataList;
    private Context mContext;

    /**
     * ListViewAdapter
     *
     * @param context
     * @param listDataList
     */
    public ListViewAdapter(Context context, List<ListData> listDataList) {
        this.mContext = context;
        this.mListDataList = listDataList;
    }

    /**
     * getCount
     *
     * @return int
     */
    @Override
    public int getCount() {
        return mListDataList.size();
    }

    /**
     * getItem
     *
     * @param position
     * @return ListData
     */
    @Override
    public ListData getItem(int position) {
        ListData listData = null;
        if (mListDataList != null && position >= 0 && position < mListDataList.size()) {
            listData = mListDataList.get(position);
            return listData;
        }
        return listData;
    }

    /**
     * getItemId
     *
     * @param position
     * @return long
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     *
     * @param position
     * @param convertComponent
     * @param componentContainer
     * @return Component
     */
    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer componentContainer) {
        final Component cpt;
        if (convertComponent == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_list_tag, null, false);
        } else {
            cpt = convertComponent;
        }
        final TagView tagView = (TagView) cpt.findComponentById(ResourceTable.Id_tagview_item_list);

        List<Tag> tags = new ArrayList<>();
        Tag tag = new Tag(getItem(position).getText());
        tag.layoutColor = Color.getIntColor("#F06292");
        tags.add(tag);

        tag = new Tag(getItem(position).getText());
        tag.layoutColor = Color.getIntColor("#90CAF9");
        tags.add(tag);

        tag = new Tag(getItem(position).getText());
        tag.layoutColor = Color.getIntColor("#80DEEA");
        tags.add(tag);
        tagView.removeAllTags();
        tagView.addTags(tags);
        return cpt;
    }
}
